<?php
class News extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->helper('url_helper');
    }

    public function index()
    {
        $data['news'] = $this->news_model->get_news();
        //将数据传递给视图
        $data['title'] = 'News archive';
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }


    public function view($slug = NULL)
    {
        $data['news_item'] = $this->news_model->get_news($slug);

        if (empty($data['news_item']))
        {
            show_404();
        }

        $data['title'] = $data['news_item']['title'];

        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
    }


    public function create()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', '标题', 'required|min[3]|max[255]',
            array('required' => 'title name is required')
        );
        $this->form_validation->set_rules('text', '内容', 'required',
            array('required' => 'text name is required')
        );

        if ($this->form_validation->run() === FALSE){
            $this->load->view('templates/header', ['title' => 'Create a news item']);
            $this->load->view('news/create');
            $this->load->view('templates/footer');

        }else{
            $data = $this->input->post();
            $attr_id = $this->news_model->add($data);

            if($attr_id){   
                echo "<script>alert('添加成功');location.href='select'</script>";
            }else{   
                echo "<script>alert('添加成功');location.href='index'</script>";
            }

            //'title' => $this->request->getVar('title'),
            //'slug'  => url_title($this->request->getVar('title')),
            //'text'  => $this->request->getVar('text'),
            $this->load->view('news/success');
        }
    }
//     //查询
//     public function select()
//     {
//         $data=$this->user->getAll();
//         //print_r($data);die;
//         $this->load->view('index/show.html',['date'=>$data]);
//     }
//     //删除
//     public function delete()
//     {
//         $id=$_GET['id'];
//         $res=$this->user->delete($id);
//         if($res){
//             echo "<script>alert('删除成功');location.href='select'</script>";
//         }
//         else{
//           echo "<script>alert('删除成功');location.href='select'</script>";

//         }
//     }
//     //修改的数据
//     public function update()
//     {
//         $id=$_GET['id'];
//         $data=$this->user->getOne($id);
//         $this->load->view('index/update.html',['data'=>$data]);
//     }

//     //执行修改
//     public function upd()
//     {   
//         $id=$_POST['id'];
//         $username=$_POST['username'];
//         $password=$_POST['password'];
//         $result=$this->user->status($id,$username,$password);
//         if($result)
//         {    
//             echo "<script>alert('修改成功');location.href='select'</script>";

//         }else{
//             echo "<script>alert('修改失败');location.href='update'</script>";

//         }
//     }

}